//
//  UIView+Extension.m
//  iTrends
//
//  Created by fanrui on 12-9-3.
//
//

#import "UIView+Extension.h"

@implementation UIView (Extension)
-(CGFloat)right{

    return self.originX + self.width;
}
-(CGFloat)bottom{
    return self.originY + self.height;
}
-(CGFloat)height
{
    return self.frame.size.height;
}
-(void)setHeight:(CGFloat)height
{
    [self setHeight:height Animated:NO];
}


- (CGFloat)center_Y{

    return self.center.y;
}
- (CGFloat)center_X{
        return self.center.x;
}
- (void)setCenter_Y:(CGFloat)y{

    self.center = CGPointMake(self.center.x, y);
}
- (void)setCenter_X:(CGFloat)x{
    self.center = CGPointMake(x, self.center.y);
}
- (void)shake

{
    
    [UIView animateWithDuration:.2
     
                     animations:^{
                         
                         self.transform = CGAffineTransformMakeScale(1.2, 1.2);
                         
                         
                         
                     }completion:^(BOOL finish){
                         
                         [UIView animateWithDuration:.2
                          
                                          animations:^{
                                              
                                              self.transform = CGAffineTransformMakeScale(1, 1);
                                              
                                          }completion:^(BOOL finish){
                                              
                                          }];
                     }];
}




-(void)setHeight:(CGFloat)height Animated:(BOOL)animate
{
    CGRect frame=self.frame;
    frame.size.height=height;
    if (animate) {
        [UIView animateWithDuration:.3 animations:^{
            self.frame=frame;
        }];
    }else{
        self.frame=frame;
    }
}
-(void)addHeight:(CGFloat)height
{
    [self setHeight:[self height]+height ];
}


-(CGFloat)width
{
    return self.frame.size.width;
}
-(void)setWidth:(CGFloat)width
{
    [self setWidth:width Animated:NO];
}
-(void)setWidth:(CGFloat)width Animated:(BOOL)animate
{
    CGRect frame=self.frame;
    frame.size.width=width;
    if (animate) {
        [UIView animateWithDuration:.3 animations:^{
            self.frame=frame;
        }];
    }else{
        self.frame=frame;
    }

}
-(void)addWidth:(CGFloat)width
{
    [self setWidth:[self width]+width Animated:NO];
}

-(CGFloat)originX
{
    return self.frame.origin.x;
}
-(void)setOriginX:(CGFloat)x
{
    [self setOriginX:x Animated:NO];
}
-(void)setOriginX:(CGFloat)x Animated:(BOOL)animate
{
    CGRect frame=self.frame;
    frame.origin.x=x;
    if (animate) {
        [UIView animateWithDuration:.3 animations:^{
            self.frame=frame;
        }];
    }else{
        self.frame=frame;
    }

}
-(void)addOriginX:(CGFloat)x
{
    [self setOriginX:[self originX]+x];
}

-(CGFloat)originY
{
    return self.frame.origin.y;
}
-(void)setOriginY:(CGFloat)y
{
    [self setOriginY:y Animated:NO];
}
-(void)setOriginY:(CGFloat)y Animated:(BOOL)animate
{
    CGRect frame=self.frame;
    frame.origin.y=y;
    if (animate) {
        [UIView animateWithDuration:.3 animations:^{
            self.frame=frame;
        }];
    }else{
        self.frame=frame;
    }

}
-(void)addOriginY:(CGFloat)y
{
    [self setOriginY:[self originY]+y];
}

-(CGSize)size
{
    return self.frame.size;
}
-(void)setSize:(CGSize)size
{
    [self setSize:size Animated:NO];
}
-(void)setSize:(CGSize)size Animated:(BOOL)animate
{
    CGRect frame=self.frame;
    frame.size=size;
    if (animate) {
        [UIView animateWithDuration:.3 animations:^{
            self.frame=frame;
        }];
    }else{
        self.frame=frame;
    }
}

-(CGPoint)origin
{
    return self.frame.origin;
}
-(void)setOrigin:(CGPoint)point
{
    [self setOrigin:point Animated:NO];
}
-(void)setOrigin:(CGPoint)point Animated:(BOOL)animate
{
    CGRect frame=self.frame;
    frame.origin=point;
    if (animate) {
        [UIView animateWithDuration:.3 animations:^{
            self.frame=frame;
        }];
    }else{
        self.frame=frame;
    }

}
-(CGPoint)originTopRight
{
    return CGPointMake(self.origin.x+self.width, self.origin.y);
}

-(CGPoint)originBottomLeft
{
    return CGPointMake(self.originX, self.originY+self.height);
}

-(CGPoint)originBottomRight
{
    return CGPointMake(self.originX+self.width, self.originY+self.height);
}
-(CGRect)rectForCenterofSize:(CGSize)size
{
    CGRect rect;
    rect.size.width=size.width;
    rect.size.height=size.height;
    rect.origin.x=(self.width-size.width)/2.0;
    rect.origin.y=(self.height-size.height)/2.0;
    return rect;
}

-(NSArray*)subviewsWithClass:(Class )cls
{
    NSArray *array=[self subviews];
    return [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        if ([evaluatedObject isKindOfClass:cls]) {
            return YES;
        }
        return NO;
    }]];
}
- (BOOL)shadowOff
{
    return self.shadowOff;
}
- (void)setShadowOff:(BOOL)shadowOff
{
    if (shadowOff) {
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, -4);
        self.layer.shadowOpacity = 0.2;
        self.layer.masksToBounds = NO;
        self.clipsToBounds = false;
    }
}

-(id)initWithFrame:(CGRect)frame nibNameOrNil:(NSString *)nibNameOrNil
{
	if(self.layer==nil){
		self=[self initWithFrame:frame];
	}else{
		self.frame=frame;
	}
    if (self) {
        if (nibNameOrNil==nil||[nibNameOrNil isEqualToString:@""]) {
            nibNameOrNil=NSStringFromClass([self class]);
        }
        UIView *view=[[UINib nibWithNibName:nibNameOrNil bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
		view.frame=self.bounds;
		self.backgroundColor=[UIColor clearColor];
		if([self isKindOfClass:[UITableViewCell class]])
		{
			self.frame=frame;
			[[(UITableViewCell*)self contentView] setFrame:self.bounds];
			[[(UITableViewCell*)self contentView] setBackgroundColor:[UIColor clearColor]];
			[[(UITableViewCell*)self contentView] addSubview:view];
		}else{
			view.frame=self.bounds;
			[self addSubview:view];
		}
    }
    return self;
}
-(UIImage *)convertViewToImage

{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}
- (UIView * (^)(CGFloat))cornerRadius{
    return ^UIView * (CGFloat cornerRadius){
        
        if (self == nil) {
            return nil;
        }

        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;
        return self;
    };
}
- (UIView * (^)(CGFloat))borderWidth{
    return ^UIView * (CGFloat borderWidth){
        
        self.layer.borderWidth = borderWidth;
    
        return self;
    };
}
- (UIView * (^)(UIColor *))borderColor{
    return ^UIView * (UIColor * borderColor){
        
        self.layer.borderColor = borderColor.CGColor;
        
        return self;
    };

}

- (void)cutRoundView {
    CGFloat corner = self.frame.size.width / 2;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(corner, corner)];
    shapeLayer.path = path.CGPath;
    self.layer.mask = shapeLayer;    
}
- (UIImage *)captureView
{
    UIImage* image = nil;
    if ([self isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)self;
        UIGraphicsBeginImageContext(scrollView.contentSize);
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;
        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;
        UIGraphicsEndImageContext();
    }else{
//        CGRect rect = self.frame;
//        UIGraphicsBeginImageContext(rect.size);
//        CGContextRef context = UIGraphicsGetCurrentContext();
//        [self.layer renderInContext:context];
//        image = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
        
        
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.layer renderInContext:context];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
    }
    if (image != nil) {
        return image;
    }
    return nil;
}
+(instancetype)loadViewForNib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil]firstObject];
}
+(instancetype)loadViewForNibWithName:(NSString *)name
{
    return [[[NSBundle mainBundle] loadNibNamed:name owner:self options:nil]firstObject];
}
@end
